package com.vinaprime.primeschedule.presentation;

import java.io.File;
import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ShareActionProvider;
import android.widget.Toast;

import com.vinaprime.primeschedule.businesslayer.DatabaseController_BL;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
	
	// references to our images
    private Integer[] _image = {
    		R.raw.menu_projects,
    		R.raw.menu_tasks,
    		R.raw.menu_events,
            R.raw.menu_contacts,
            R.raw.menu_contacts_groups,
            R.raw.menu_sounds,
            R.raw.menu_helps,
            R.raw.menu_abouts,
            R.raw.menu_settings,
            
    };
    private String[] _str ;
    GridView gridView;
    GridViewAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ActionBar actionBar = getActionBar();
		Drawable d=getResources().getDrawable(R.drawable.background_top_shape_green);  
		actionBar.setBackgroundDrawable(d);

		actionBar.setDisplayShowHomeEnabled(true);
		actionBar.setDisplayUseLogoEnabled(false);
		//Configuration newConfig = new Configuration();
		//lockScreenRotation(Configuration.ORIENTATION_PORTRAIT);
		
	      _str = new String[]{
				 
	    		getResources().getString(R.string.menu_Projects),
	    		getResources().getString(R.string.menu_Tasks),
	    		getResources().getString(R.string.menu_Events),
	    		getResources().getString(R.string.menu_Contacts),
	    		getResources().getString(R.string.menu_ContactsGroup),
	    		getResources().getString(R.string.menu_Sounds),
	    		getResources().getString(R.string.menu_Helps),
	    		getResources().getString(R.string.menu_About),
	    		getResources().getString(R.string.menu_Settings),
		};
		gridView = (GridView) findViewById(R.id.gridViewMenu);
		adapter = new  GridViewAdapter(_str, _image,this);
		gridView.setAdapter(adapter);
		
		
		gridView.setOnItemClickListener(new OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		    	switch(position){
		        case 0: 
		          /** Projects */
		          //Toast.makeText(getBaseContext(), "Projects", Toast.LENGTH_SHORT).show();
		          Intent intentProject = new Intent(getApplicationContext(), VpprojectExpandableActivity.class);
		          startActivity(intentProject);
		          break;
		        case 1:
		        	/** Tasks */
		        	Intent intentTaskList = new Intent(getApplicationContext(), VPTaskListActivity.class);
		        	startActivity(intentTaskList);
			          break;
		        case 2:
		        	/** Events */
		        	Intent intentEvent = new Intent(getApplicationContext(), VPEventActivity.class);
		        	startActivity(intentEvent);
			          break;
		        
		        case 3:
		        	/** Contacts */
		        	Intent intentContact = new Intent(getApplicationContext(), VPContactActivity.class);
		        	startActivity(intentContact);
			         // Toast.makeText(getBaseContext(), "Contacts", Toast.LENGTH_SHORT).show();	
			          break;
		        
		        case 4:
		        	/** Contact groups */
		        	Intent intentContactGroup = new Intent(getApplicationContext(), VPContactGroupActivity.class);
		        	startActivity(intentContactGroup);
			        
		        	//Toast.makeText(getBaseContext(), "Contact groups", Toast.LENGTH_SHORT).show();
			          break;
		        case 5:
		        	/** Sounds */
			         // Toast.makeText(getBaseContext(), "Sounds", Toast.LENGTH_SHORT).show();
			          Intent intentSnoozeSound = new Intent(getApplicationContext(), VPSnoozeSoundActivity.class);
			          startActivity(intentSnoozeSound);
			          break;
		        case 6:
		        	/** Helps */
			          Toast.makeText(getBaseContext(), "Helps", Toast.LENGTH_SHORT).show();
			          break;
		        case 7:
		        	/** Abouts */
			          Toast.makeText(getBaseContext(), "Abouts", Toast.LENGTH_SHORT).show();
			          break;
		        case 8:
		        	/** Settings */
			          Intent intentSetting = new Intent(getApplicationContext(), SettingActivity.class);
			          startActivity(intentSetting);
			          break;
		        case 9:
			          /** Start a new Activity MyCards.java */
		        	/** Projects */
			          Toast.makeText(getBaseContext(), "Null", Toast.LENGTH_SHORT).show();
			          break;
		        case 10:
		        	/** Projects */
			          Toast.makeText(getBaseContext(), "Null", Toast.LENGTH_SHORT).show();
		          break;
		    	}
		    }
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	

	public void click(View v) throws IOException
	{
		PackageManager pm = this.getPackageManager();
		String path = "";		
		for (String name:getAssets().list(""))
		{
			System.out.println(name);
		}
		
		try
		{
			PackageInfo pi = pm.getPackageInfo(this.getPackageName(), 0);
			path = pi.applicationInfo.dataDir;
			path = path + "/";
		}
		catch(NameNotFoundException e)
		{
			Log.w("yourtag", "Error Package name not found ", e);
		}
		
		//File f = new File(path + "databases/DB_TaskList.db");
		File f = new File(path + "databases/DB_TaskList.db");
		if(f.exists())
		{
			Log.v("File Message", "Exist");
		}
		else
		{
			Log.v("File Message", "Not Exist");
		}
		
		//DatabaseController_DL db = new DatabaseController_DL(this, path  + "databases/");
		DatabaseController_BL db = new DatabaseController_BL(getApplicationContext(), path  + "databases/");
		//ConfigDatabase db = new ConfigDatabase(this, dbControllerBL.getDatabaseName(this), dbControllerBL.getDatabaseVersion(this));
		
		if(db.checkDatabaseExist())
		{
			Log.v("Message from Database", "Exist");
		}
		else
		{
			Log.v("Message from Database", "Not Exist");
			//db.onCreate(null);
			db.copyDatabase();
		}
		
		System.out.println("Succesfull");
		/*
		Cursor cursor = db.readData(this, path  + "databases/");
		String customer_number = cursor.getString(
			    cursor.getColumnIndexOrThrow("customer_number")
			);
			
			TextView txt = (TextView)findViewById(R.id.textView1);
			
			txt.setText(customer_number);*/
	}
		
	
	public void send_message(View view) {
	    	Intent intent = new Intent(this, VPContactActivity.class);	 
	    	startActivity(intent);		
	}
	
}
